package com.hong.spring.controller;

import com.hong.spring.api.IUserService;
import com.hong.spring.entity.User;
import com.hong.spring.utils.DataResponse;
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: csh
 * @Date: 2020/8/18 16:11
 * @Description:
 */
@RestController
@RequestMapping("/")
@Log4j2
public class UserController {

    //注意这个directUrl必须写，因为这个是getHost的地址可能获取出错，就导致不是127.0.0.1。。。坑！
    @MotanReferer(basicReferer = "motantestClientBasicConfig", directUrl = "127.0.0.1:8081")
    private IUserService userService;

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @ResponseBody
    public DataResponse<User> user(Integer id){
        try {
            return userService.findById(id);
        }catch (Exception e){
            log.error("获取出错!{}",e);
        }
        return DataResponse.BuildFailResponse("获取出错！");
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    public DataResponse<List<User>> findAll(){
        try {
            return userService.findByAll();
        }catch (Exception e){
            log.error("获取出错!{}",e);
        }
        return DataResponse.BuildFailResponse("获取出错！");
    }
}
